<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order', function (Blueprint $table) {
            $table->id();
            $table->bigInteger("user_id")->default(0);
            $table->char('org_root',3)->nullable()->comment('顶级机构');
            $table->char('org_code',11)->nullable()->comment('所属机构');
            $table->char("orderno", 64)->default('')->comment("订单号");
            $table->unsignedSmallInteger('category')->default(0)->comment("0 充值次数");
            $table->integer('ticket')->default(0)->comment("获得次数");
            $table->unsignedTinyInteger("channel")->default(0)->comment("支付渠道 0 余额， 1 微信");
            $table->unsignedTinyInteger("status")->default(0)->comment("0 支付中 1已支付 2过期未支付 3支付失败");
            $table->timestamps();
            $table->unique(["orderno"]);
            $table->index(["user_id"]);
        });

        Schema::create('users_money_withdraw', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger("user_id")->comment("用户id");
            $table->char('org_root',3)->nullable()->comment('顶级机构');
            $table->char('org_code',11)->nullable()->comment('所属机构');
            $table->decimal('money', 10)->default(0)->comment("金额数");
            $table->tinyInteger('status')->default(0)->comment("提现结果: -1 提现失败, 0 提现中, 1 提现成功 ");
            $table->mediumText('comment')->comment("备注");
            $table->timestamps();
            $table->index("user_id");
        });
        DB::statement("ALTER TABLE `users_money_withdraw` COMMENT='用户提现申请表'");

        Schema::table('users', function (Blueprint $table) {
            $table->timestamp('expired_at')->nullable()->comment("次数过期时间")->after('remember_token');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order');
        Schema::dropIfExists('users_money_withdraw');
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn(['expired_at']);
        });
    }
};
